<template>
	<view class="pd16_15">
		<radio-group @change="changeNum">
			<view class="flex wrap space mt16">
				<view v-for="(item,index) in moneyList" :key="index" :class="index > 1 ? 'mt16' : ''"
					class="box pd20_15 flex alcenter space" style="width: 330rpx;">
					<text class="ft16 cl-main ftw600">¥{{item.price}}</text>
					<radio :value="index" :checked="num == item.price" :color="tempColor" />
				</view>
			</view>
		</radio-group>
		<view class="form-footer-h">
			<view class="form-footer form-footer-h">
				<view class="form-footer-main pd10_15">
					<button style="width: 100%; float: right;" @click="paySuccess" class="btn-big"
						:style="getBtnStyle">支付 {{num>0?'¥'+num:''}}</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import hFormAlert from '@/components/h-form-alert/h-form-alert.vue';
	export default {
		components: {
			hFormAlert
		},
		data() {
			return {
				num: '',
				moneyList: [],
				tdnum: '901',
				openid: '',
				tdList: [{
						num: '901',
						name: '微信H5(50-100-200)'
					},
					{
						num: '904',
						name: '支付宝H5(30-50-100-200)'
					},
				],
				numa: '',
				userinfo: '',
				showCouponInvite: false,
				cancelShow: false,
				normal_coin: '',
				forever_coin: ''
			}
		},
		onLoad(e) {
			if (e.price) {
				this.num = e.price
			}
			//#ifdef MP-WEIXIN
			this.wxlogin()
			//#endif
			this.rechargeRule()
		},
		onShow() {
			this.ongrzlTap();
		},
		methods: {
			rechargeRule() {
				uni.request({
					url: this.configs.webUrl + '/api/recharge/rechargeRule',
					success: (res) => {
						console.log(res);
						this.moneyList = res.data.data
					}
				})
			},
			changeNum(e) {
				console.log(e);
				let value = this.moneyList[e.detail.value]
				console.log(value);
				this.num = value.price
				this.normal_coin = value.give_coin
				this.forever_coin = value.forever_coin
			},
			paySuccess() {
				var this_=this
				wx.login({
				  success (res) {
				    if (res.code) {
				      //发起网络请求
				      uni.request({
				            url: this_.configs.webUrl+'/api/virtual_payment/getPayOrder',  
				            method:'GET',  
				            data: {   
				                token: uni.getStorageSync("userinfo").token,       //你的小程序的secret,  
				                code: res.code,                                      //wx.login 登录成功后的code  
								buy_num: this_.num,
								forever_coin: this_.forever_coin,
								normal_coin: this_.normal_coin
							},  
				            success: (cts) => { 
								
								var pay_res = cts.data;
								
				                if(pay_res.code == 200)
								{
									wx.requestVirtualPayment({
										signData: JSON.stringify({
											offerId: pay_res.data.signData.offerId,
											buyQuantity: pay_res.data.signData.buyQuantity,
											env: pay_res.data.signData.env,
											currencyType: 'CNY',
											platform: pay_res.data.signData.platform,
											productId: pay_res.data.signData.productId,
											goodsPrice: pay_res.data.signData.goodsPrice,
											outTradeNo: pay_res.data.signData.outTradeNo,
											attach: pay_res.data.signData.attach,
										}),
										paySig: pay_res.data.pay_sing,
										signature: pay_res.data.signature,
										mode: 'short_series_coin',
										success(res_msg) {
											console.log('requestVirtualPayment success', res_msg)
											uni.request({
												url: this_.configs.webUrl+'/api/virtual_payment/rechargePaySuccess',
												method:'POST',  
												data: {   
												    token: uni.getStorageSync("userinfo").token,       //你的小程序的secret,  
												    trade_no: pay_res.data.signData.outTradeNo,
													pay_sing:pay_res.data.pay_sing,	
													signature:pay_res.data.signature								
												},success: (cts) => { 
													console.log(99999999999, '支付成功');
													uni.navigateBack()
												},fail: (err1) => {
													console.log(err1);
														//失败的回调
												}
											})
										},
										fail({
											errMsg, errCode
										}) {
											console.error(errMsg, errCode)
										},
									})
								}
				            },
				      	  fail: (err1) => {
				      	  	console.log(err1);
				      	     	//失败的回调
				      	  }
				      });  
					  
				    } else {
				      console.log('登录失败！' + res.errMsg)
					  uni.showToast({ title: res.errMsg,icon:"none" });
				    }
				  }
				})
			},
			async ongrzlTap() {
				let data = {};
				data.token = uni.getStorageSync("userinfo").token;
				data.uid = uni.getStorageSync("userinfo").id;
				uni.request({
					url: this.configs.webUrl + '/api/user/index',
					data: data,
					success: res => {
						if (res.data.code == 1) {
							let ionfo = res.data.data
							ionfo.isLogin = true
							this.userinfo = ionfo
							this.jfdt = ionfo.config.jfdt ? ionfo.config.jfdt : ''
							uni.setStorage({ //缓存配置信息
								key: 'userinfo',
								data: res.data.data
							})
							if (res.data.data.avatar) {
								var str = res.data.data.avatar;
								if (str.indexOf("data:image") != -1) {
									this.avatar = '';
								} else {
									if (str.indexOf("http") != -1) {
										this.avatar = res.data.data.avatar;
									} else {
										this.avatar = this.configs.imgUrl + res.data.data.avatar;
									}
								}
							} else {
								this.avatar = '';
							}
							uni.setStorage({ //缓存配置信息
								key: 'avatar',
								data: this.avatar
							})
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: "none"
							});
						}
					},
					fail: (data, code) => {
						//console.log('fail' + JSON.stringify(data));
					}
				});
			},
			wxlogin() {
				var this_ = this
				uni.login({
					timeout: 10000,
					provider: 'weixin', //如果是uniapp，在这里需要标明平台的类型，支持的参数请查阅uniapp官网的uni.login()文档
					success: (res) => {
						//console.log(res);
						//登陆成功的回调
						uni.request({
							url: this.configs.webUrl + '/api/user/getOpenid',
							method: 'GET',
							data: {
								token: uni.getStorageSync("userinfo").token, //你的小程序的secret,  
								code: res.code //wx.login 登录成功后的code  
							},
							success: (cts) => {
								console.log(cts.data.data);
								if (cts.data.code == 1) {
									this_.openid = cts.data.data.openid
								} else {
									uni.showModal({
										title: '温馨提示',
										content: JSON.stringify(cts.data),
										showCancel: false,
										confirmText: "确定",
										success: function(res) {
											if (res.confirm) {
												uni.navigateBack();
											} else if (res.cancel) {

											}
										}
									});
								}
							},
							fail: (err1) => {
								console.log(err1);
								//失败的回调
							}
						});

					},
					fail: (err) => {
						console.log(err);
						//登陆失败的回调
					}
				})
			}
		}
	}
</script>

<style>
	.tag-coupon {
		width: 100%;
		height: 80rpx;
		border-radius: 16rpx;
		text-align: center;
		line-height: 80rpx;
		font-size: 28rpx;
	}

	.recharge-header {
		height: 240rpx;
		width: 100%;
		position: relative;
	}

	.recharge-header image {
		width: 100%;
		height: 240rpx;
	}

	.recharge-header .main {
		position: absolute;
		width: 100%;
		height: 240rpx;
		left: 0;
		top: 0;
	}

	.tdadf {
		border: 1px solid #f8f8f8;
		padding: 10px 10px;
		text-align: center;
		height: 80upx;
		background: #ffffff;
		border-radius: 8px;
		box-shadow: 0px 4px 20px 0px rgba(0, 0, 0, 0.04);
		font-size: 30upx;
		color: #666;
	}
</style>